package com.xju.atpgs.generator;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Population {
    private List<Paper> papers;

    public List<Paper> getPapers() {
        return papers;
    }

    public void setPapers(List<Paper> papers) {
        this.papers = papers;
    }

    public Population(int size) {
        papers=new ArrayList<>();
        for (int i=0;i<size;i++){
            papers.add(null);
        }
    }

    public Population(GeneticParameter geneticParameter) {
        List<Paper> papers=new ArrayList<>();
        Random random = new Random();
        List<Integer> choice = new ArrayList<Integer>();
        List<Integer> judge = new ArrayList<Integer>();
        List<Integer> filling = new ArrayList<Integer>();
        List<Integer> shortAnswer = new ArrayList<Integer>();
        List<Integer> solve = new ArrayList<Integer>();
        double difficulty=0;
        double adaptationDegree=0;
        for (int i = 0; i < geneticParameter.getGROUP_SIZE(); i++) {
            choice.clear();
            while (choice.size() < geneticParameter.getChoiceNum()) {
                int listSize = geneticParameter.getChoiceQuestion().size();
                int index = random.nextInt(listSize);
                if (!choice.contains(index)) {
                    choice.add(index);
                    difficulty+=Double.parseDouble(geneticParameter.getChoiceQuestion().get(index).getQuesLevel())*geneticParameter.getChoiceScore();
                }

            }
            judge.clear();
            while (judge.size() < geneticParameter.getJudgeNum()) {
                int listSize = geneticParameter.getJudgeQuestion().size();
                int index = random.nextInt(listSize);
                if (!judge.contains(index)) {
                    judge.add(index);
                    difficulty+=Double.parseDouble(geneticParameter.getJudgeQuestion().get(index).getQuesLevel())*geneticParameter.getJudgeScore();
                }
            }
            filling.clear();
            while (filling.size() < geneticParameter.getFillingNum()) {
                int listSize = geneticParameter.getFillingQuestion().size();
                int index = random.nextInt(listSize);
                if (!filling.contains(index)) {
                    filling.add(index);
                    difficulty+=Double.parseDouble(geneticParameter.getFillingQuestion().get(index).getQuesLevel())*geneticParameter.getFillingScore();
                }
            }
            shortAnswer.clear();
            while (shortAnswer.size() < geneticParameter.getShortAnswerNum()) {
                int listSize = geneticParameter.getShortAnswerQuestion().size();
                int index = random.nextInt(listSize);
                if (!shortAnswer.contains(index)) {
                    shortAnswer.add(index);
                    difficulty+=Double.parseDouble(geneticParameter.getShortAnswerQuestion().get(index).getQuesLevel())*geneticParameter.getShortAnswerScore();
                }
            }
            solve.clear();
            while (solve.size() < geneticParameter.getSolveNum()) {
                int listSize =geneticParameter.getSolveQuestion().size();
                int index = random.nextInt(listSize);
                if (!solve.contains(index)) {
                    solve.add(index);
                    difficulty+=Double.parseDouble(geneticParameter.getSolveQuestion().get(index).getQuesLevel())*geneticParameter.getSolveScore();
                }
            }
            difficulty=difficulty/geneticParameter.getTotalScore();
            adaptationDegree=1-Math.abs(geneticParameter.getEXPECT()-difficulty);
            Paper paper=new Paper(adaptationDegree,difficulty,choice,judge,filling,shortAnswer,solve,geneticParameter);
            papers.add(paper);
        }
        this.papers=papers;
    }

    public Paper getBest(){
        Paper paper=papers.get(0);
        for (int i=1;i<papers.size();i++){
            if(paper.getAdaptationDegree()<papers.get(i).getAdaptationDegree()){
                paper=papers.get(i);
            }
        }
        return paper;
    }

    public Paper getPaper(int index){
        return papers.get(index);
    }

    public void setPaper(int index,Paper paper){
        papers.set(index,paper);
    }

    public int getLength(){
        return papers.size();
    }
}
